package com.shenzhenai.composebasic.components

import android.Manifest
import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.google.accompanist.permissions.PermissionStatus
import com.google.accompanist.permissions.isGranted
import com.google.accompanist.permissions.rememberPermissionState
import com.google.accompanist.permissions.shouldShowRationale
import kotlinx.coroutines.launch

/**
 * @author huangzehui
 * @date 2025/9/18 13:19 周四
 * @description
 *
 *
 * 权限
 * https://www.bilibili.com/video/BV1v3411W7Di?vd_source=e07cc8cc94eb22bd76722071b03cd199&spm_id_from=333.788.videopod.sections
 */

@SuppressLint("CoroutineCreationDuringComposition")
@OptIn(ExperimentalMaterial3Api::class, ExperimentalPermissionsApi::class)
@Composable
fun PermissionSample() {
    var startTime by remember {
        mutableStateOf(0L)
    }

    // 权限状态对象
    val permissionState =
        rememberPermissionState(permission = Manifest.permission.CAMERA, onPermissionResult = {

            if (!it) {
                if (System.currentTimeMillis() - startTime <= 200) {
                    Log.e("young_testdd","永久拒绝")
                }
            }
            Log.e("young_testdd", "it = ${it} = ${System.currentTimeMillis() - startTime}ms")
        })

    val coroutineScope = rememberCoroutineScope()

    Scaffold(topBar = {
        TopAppBar(title = { Text(text = "Permission Demo") })
    }) {
        Column(
            modifier = Modifier
                .padding(it)
                .fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {

            when (permissionState.status) {
                PermissionStatus.Granted -> {
                    Text(text = "已经同意了相机权限")
                }

                is PermissionStatus.Denied -> {
                    Column(
                        verticalArrangement = Arrangement.Center,
                        horizontalAlignment = Alignment.CenterHorizontally
                    ) {
                        val text = if (permissionState.status.shouldShowRationale) {
                            // 已经点击获取权限，此时拒绝
                            "相机权限已拒绝，点击按钮再次请求"
                        } else {
                            // 默认情况下的拒绝
                            "相机权限已被禁止"
                        }
                        Text(text = text)

                        Log.e(
                            "young_testdd",
                            "permissionState.status.isGranted = ${permissionState.status.isGranted}"
                        )

                        Button(onClick = {
                            startTime = System.currentTimeMillis()
                            permissionState.launchPermissionRequest()
                        }) {
                            Text(text = "点击获取权限")
                        }
                    }

                }
            }

//            if (permissionState.status.isGranted) {
//                Text(text = "已授权")
//            } else {
//                if (permissionState.status.shouldShowRationale) {
//                    Text(text = "拒绝权限，需要弹出说明弹出框", modifier = Modifier.clickable {
//                        coroutineScope.launch {
//                            permissionState.launchPermissionRequest()
//                        }
//                    })
//                } else {
//                    Text(text = "拒绝权限，不需要弹出说明弹出框", modifier = Modifier.clickable {
//                        coroutineScope.launch {
//                            permissionState.launchPermissionRequest()
//                        }
//                    })
//                }
//            }
        }
    }
}


@Preview
@Composable
fun PermissionSamplePreview() {
    PermissionSample()
}